package com.abel.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.BindException;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public abstract class BaseMultiActionController extends MultiActionController {

	/**
	 * This method will give you the option of using the bindexception feature
	 * to show errors and use different validation classes for different methods
	 * 
	 * @param request
	 * @param command
	 * @param validator
	 * @return
	 * @throws Exception
	 */
	public BindException bindObject(HttpServletRequest request,
			Object command, Validator validator) throws Exception {

		ServletRequestDataBinder binder = createBinder(request, command);
		binder.bind(request);
		BindException errors = new BindException(binder.getBindingResult());
		if (validator.supports(command.getClass())) {
			ValidationUtils.invokeValidator(validator, command, errors);
		}
		return errors;
	}
}
